function showstatus(){
	$("flashcontent").setStyle("display", "none");
	$("log").setStyle("display", "block");
}
function updatestatus(str){
	//$("flashcontent").setStyle("display", "none");
	//$("log").setStyle("display", "block");
	$("log").innerHTML = str;
}
function playVideo(video_id, title){

	var tube_url = "http://www.youtube.com/watch?v="+video_id;
	updatestatus("Getting fullscreen video.");
	/*try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	} catch (e) {
		alert("Permission UniversalBrowserRead denied.");
	}*/
	var myAjax = new Ajax(PROXY_URL ? PROXY_URL : tube_url, {
		method: PROXY_URL ? 'POST' : 'GET',
		data: {"url":tube_url},
		onComplete: function(responseText, responseXML){
			updatestatus("Video found");
	/* when youtube changes update this regex */
			//		var fullscreenUrl = '/watch_fullscreen?fs=1&vq=None&video_id=WgffM5DfT8A&l=331&sk=l3PLhqL-kTWgDaxZyrtzDDNujvIzeqV0C&fmt_map=
		//&t=OEgsToPDskLgPQ9cq0PWlZPnltcmuxf6&hl=en&plid=AARMg-sriBRH_uEcAAAAoAAwAAA&sdetail=f%253Adir%252C&title=Tree in the Forest [captioned version]';	
		//'/watch_fullscreen?fs=1&ad_module=1&video_id=A8TwNEDo6kY&l=25
			alert(tube_url);
			var string = responseText.match(/watch_fullscreen\?fs=1&vq=None&video_id\=(.+)\'/i);
			var fullscreenurl = "http://www.youtube.com/player2.swf?v=1&video_id="+string[1];
			//alert(fullscreenurl);
			//embedSWF(fullscreenurl);
			embedTubePlayer(fullscreenurl);
			$("tx_title").innerHTML = ""+title.replace(/(<([^>]+)>)/ig,"");
		}
	}).request();
}
function embedSWF(url){
	updatestatus("Playing video.");
	var so = new SWFObject(url, "youtubevid", "100%", "100%", "8");
	//so.addParam("wmode", "transparent");
	so.write("flashcontent");
	$("flashcontent").setStyle("display", "block");
	$("log").setStyle("display", "none");
}
function embedTubePlayer(fullscreenurl){

	//fullscreenurl = 'http://www.youtube.com/player2.swf?v=1&video_id=BSYQcFi_ayY&l=384&t=OEgsToPDskKqMyYKZe0YhsTfINkkxu1q&sk=5j8b8XW5UKH3ReeFBfKJ9AC&fs=1&title="We Will Become Silhouettes" by the Postal Service';
	var matches = fullscreenurl.match(/video_id=(.+?)&/i);	
	video_id = matches[1];
	var matches = fullscreenurl.match(/t=(.+?)&/i);	
	t = matches[1];
	var matches = fullscreenurl.match(/sk=(.+?)&/i);	
	sk = matches[1];
	//alert(video_id  +" "+ t+" " +" "+ sk);
	var so = new SWFObject('http://www.philipandrews.org/itube/tubevideo.swf', "youtubevid", "100%", "100%", "8");
	so.addVariable( 'video_id', video_id );
	so.addVariable( 't', t );
	so.addVariable( 'code', sk );
	so.addVariable( 'set_volume', VOLUME );
	so.addVariable( 'video_smoothing', "true" );
	//so.addVariable( 'seek_to_sec', 3 );
	//so.addVariable( 'seek_to', "50" );
	//trace(t);
	//trace(t);
	//trace(sk);
	so.write("flashcontent");
	/*
	var url = 'swf/tubeplayer.swf?video_id=' + video_id + "&t=" + t + "&set_volume=" + VOLUME + "&video_smoothing=" + true + "&code=" + sk;
	var object = "<object id='movieObject' width='100%' height='100%'><param name='movie' value='" + url + "'>\</param><param name='wmode' value='transparent'><embed id='videoEmbed' src= " + url + " type='application/x-shockwave-flash' width='100%' height='100%' bgcolor='#000000' align='middle'></embed></object>";
	$("flashcontent").innerHTML = object;*/
	
	
	
	$("flashcontent").setStyle("display", "block");
	$("log").setStyle("display", "none");
	//document.youtubevid.SetVariable("_root:seek_to_sec", 50);
}
function searchCompleted(HTML){
	videos = new Array();

	if(!HTML) return;

	var videoMatches = HTML.match(/vEntry([^`]*?)end\s*vEntry/mgi);
	if(!videoMatches) videoMatches = HTML.match(/vEntry([^`]*?)\<\/td\>/mgi);

	var pattern1 = new RegExp("\"errorBox\"", "i");
	var notFound = HTML.match(pattern1) || HTML.match(/user\s*doesn't\s*have\s*any/i);
	if(!videoMatches || notFound)
	{
		//renderVideoGuide(null, showGuide, header, subheader);
		//return;
	}

	if(HTML.match(/class=\"pager[^>]*\>\s*Next\s*<\/a\>/i))
	{
		var currPage = HTML.match(/class=\"pagerCurr[^>]*\>\s*(\d+)/i);
		if(currPage) nextPage = 1*(currPage[1]) + 1;

	} else nextPage = -1;

	if(! videoMatches){
		updatestatus("No video found on youtube.");
		return;
	}
	
	for(var i = 0; i < videoMatches.length; i++)
	{
		var videoHTML = videoMatches[i];
		var video = new Object();

		//if(i == 0) alert(videoHTML);

		video.id = parseListID(videoHTML);
		video.title = parseListTitle(videoHTML);
		/*video.length = parseListLength(videoHTML);
		video.imageURL = parseListImageURL(videoHTML, video.id);
		video.rating = parseListRating(videoHTML);
		video.ratings = parseListRatings(videoHTML);
		video.views = parseListViews(videoHTML);*/
		video.date = parseListDate(videoHTML);
		//video.user = parseListUser(videoHTML);

		var theMatch;

		theMatch = videoHTML.match(/Added:[^>]*\>(.*)/mi);
		video.date = (theMatch) ? theMatch[1] : "";

		theMatch = video.date.match(/\((\d+)\)/);

		//this could be better...
		if(theMatch)
		{
			var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
			var date = new Date(theMatch[1]);
			date.setHours(date.getHours() + date.getTimezoneOffset()/60);
			video.date = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
		}

		//if(i == 0) {
		//	alert("printing video...");
		//	video.print();
		//}
		videos.push(video);
	}

	
	playVideo(videos[0].id, videos[0].title);
	//renderVideoGuide(videos, showGuide, header, subheader);
	//videoRequest = null;

}
function parseListID(HTML)
{
	var id = HTML.match(/watch\?v=([^"]+)/i);
	return (id) ? id[1] : "";
}
function parseListTitle(HTML)
{
	var title = HTML.match(/watch\?v=[^>]*\>(([^<]|\<[^i]).*?[^/].)\<\/a/i);
	return (title) ? title[1] : "Untitled Video";
}
function parseListDate(HTML)
{
	var date = HTML.match(/Added:[^>]*\>(.*)/mi);
	date = (date) ? date[1] : "";

	date = date.match(/\((\d+)\)/);

	//this could be better...
	if(date)
	{
		var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
		var dateObj = new Date(date[1]);
		dateObj.setHours(dateObj.getHours() + dateObj.getTimezoneOffset()/60);
		return months[dateObj.getMonth()] + " " + dateObj.getDate() + ", " + dateObj.getFullYear();
	}
	return "-";
}